package org.wisigoth.chat.swing.accountframe;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import org.wisigoth.chat.client.Wisigoth;
import org.wisigoth.chat.client.exceptions.AccountCreationException;
import org.wisigoth.chat.client.exceptions.AuthenticationException;
import org.wisigoth.chat.swing.WisigothController;

public class LoginController implements ActionListener {
	
	private Wisigoth wisigoth;
	private LoginWindow loginWindow;


	private AccountCreationFrame accountCreationFrame;
	private WisigothController parent;
	
	
	public LoginController(WisigothController parent) {
		this.parent = parent;
		this.wisigoth = parent.getSession().getWisigoth();
		this.loginWindow = new LoginWindow();
		this.loginWindow.setModel(this.wisigoth);
		this.loginWindow.getConnectButton().addActionListener(this);
		this.loginWindow.getCreateAccountButton().addActionListener(this);
		
	}
	


	
	public void manageAccountCreation() {
		
		this.loginWindow.setVisible(false);
		this.accountCreationFrame = new AccountCreationFrame();
		this.accountCreationFrame.getCreateButton().addActionListener(this);
		this.accountCreationFrame.getCancelButton().addActionListener(this);
		this.accountCreationFrame.setVisible(true);
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == this.loginWindow.getConnectButton()) {
			this.connect();
		} else if (e.getSource() == this.loginWindow.getCreateAccountButton()) {
			this.manageAccountCreation();
		} else if (e.getSource() == this.accountCreationFrame.getCreateButton()) {
			this.createAccount();
			System.out.println("Account Created");
		} else {
			this.accountCreationFrame.setVisible(false);
			this.accountCreationFrame.dispose();
			this.showLoginWindow();
		}
	}
	

	


/**
 * Attempt to create a new Wisigoth account with the username and the password field in the AccountCreationFrame
 */	
	private void createAccount() {
		String login = this.accountCreationFrame.getUserTextField().getText();
		char[] pwd = this.accountCreationFrame.getPwdPasswordField().getPassword();
		try {
			this.wisigoth.accountCreationRequest(login, pwd);
		} catch (AccountCreationException exception) {
			JOptionPane.showMessageDialog(this.accountCreationFrame,
					exception.getMessage(), "Error Message",
					JOptionPane.ERROR_MESSAGE);
			return;
		}
		if(this.accountCreationFrame.getSavePwdCheckBox().isSelected()) {
			this.wisigoth.getUserPreferences().storeLoginParameters(login, pwd);
		}else{
			this.wisigoth.getUserPreferences().storeLoginParameters(login, null);
		}
		this.accountCreationFrame.setVisible(false);
		this.accountCreationFrame.dispose();
		this.showLoginWindow();
	}
/**
 * Attempt to login with the username and the password field in the loginWindow
 */
	private void connect(){
		String login = this.loginWindow.getUserTextField().getText();
		char[] pwd = this.loginWindow.getPwdPasswordField().getPassword();
		boolean savePassword = this.loginWindow.getSavePwdCheckBox().isSelected();
		try {
			this.parent.getSession().authenticate(login, pwd);
		} catch (AuthenticationException exception) {
			JOptionPane.showMessageDialog(this.loginWindow,
					exception.getMessage(), "Error Message",
					JOptionPane.ERROR_MESSAGE);
			return;
		}
		if (savePassword){
			this.wisigoth.getUserPreferences().storeLoginParameters(login, pwd);
		}
		this.startWisigothSession();
	}
	
	public void startWisigothSession() {
		this.hideLoginWindows();
		this.parent.setContactListVisible(true);
	}
	
	public LoginWindow getLoginWindow() {
		return this.loginWindow;
	}
	
	public void showLoginWindow() {	
		this.loginWindow.setVisible(true);
	}

	public void hideLoginWindows() {
		this.loginWindow.setVisible(false);
		this.loginWindow.dispose();
	}
	
	

}
